package com.to.aboomy.bannersample.pagetransformer;

import com.to.aboomy.banner.BannerViewPager;
import ohos.agp.components.Component;

/**
 * @author xujianhong
 * @since 2021-08-07
 */
public class RotateUpPageTransformer implements BannerViewPager.PageTransformer{
    private static final float ANGLE = 15f;
    @Override
    public void transformerPage(BannerViewPager.PageType pageType, Component page, float offset, float offsetPixels) {
        {
            float angle = ANGLE;
            float height = 0;
            switch (pageType) {
                case PREVIOUS:
                    if (offsetPixels <= 0) {
                        float pivotX = page.getWidth() - page.getWidth() * offset * 0.6f;
                        page.setPivot(pivotX, height);
                        angle = ANGLE * (1 - offset);
                    } else {
                        angle = ANGLE;
                    }
                    break;
                case CURRENT:
                    // 往下一页滑入
                    if (offsetPixels > 0) {
                        float pivotX = page.getWidth() / 2.5f + page.getWidth() * offset * 0.6f;
                        page.setPivot(pivotX, height);
                        angle = (ANGLE * offset);
                    } else {
                        float pivotX = page.getWidth() - (page.getWidth() / 2.5f) - page.getWidth() * offset * 0.6f;
                        page.setPivot(pivotX, height);
                        angle = -ANGLE * offset;
                    }
                    break;
                case NEXT:
                    if (offsetPixels >= 0) {
                        page.setPivot(page.getWidth() * offset * 0.6f, height);
                        angle = -ANGLE * (1 - offset);
                    } else {
                        angle = -ANGLE;
                    }
                    break;
                case OTHER:
                    if (offsetPixels > 0) {
                        page.setPivot(0, height);
                        angle = -ANGLE;
                    } else {
                        page.setPivot(page.getWidth(), height);
                        angle = ANGLE;
                    }
                    break;
            }
            page.setRotation(angle);
        }
    }
}
